package com.cv.system.controller.system.user;

import com.cv.framework.common.pojo.PageResult;
import com.cv.framework.common.pojo.Result;
import com.cv.framework.operatelog.core.annotations.OperateLog;
import com.cv.framework.role.core.annotations.RolePermit;
import com.cv.system.controller.system.user.vo.user.UserPageReqVO;
import com.cv.system.controller.system.user.vo.user.UserRespVO;
import com.cv.system.controller.system.user.vo.user.UserSaveReqVO;
import com.cv.system.controller.system.user.vo.user.UserUpdateStatusReqVO;
import com.cv.system.service.user.SystemUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static com.cv.framework.common.pojo.Result.success;

/**
 * 后台管理系统 - 用户
 *
 * @author Charles_XDXD
 */
@Tag(name = "后台管理系统 - 用户")
@RestController
@RequestMapping("/system/user")
@Validated
public class SystemUserController {

    @Resource
    private SystemUserService systemUserService;

    @PostMapping("/create")
    @Operation(summary = "新增用户")
    public Result<Boolean> createUser(@Valid @RequestBody UserSaveReqVO reqVO) {
        systemUserService.createUser(reqVO);
        return success(true);
    }

    @PutMapping("update")
    @Operation(summary = "修改用户")
    @RolePermit(values = "super_admin")
    public Result<Boolean> updateUser(@Valid @RequestBody UserSaveReqVO reqVO) {
        systemUserService.updateUser(reqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @RolePermit(values = "super_admin")
    public Result<Boolean> deleteUser(@RequestParam("id") Long id) {
        systemUserService.deleteUser(id);
        return success(true);
    }

    @PutMapping("/update-status")
    @Operation(summary = "修改用户状态")
    @RolePermit(values = "super_admin")
    public Result<Boolean> updateUserStatus(@Valid @RequestBody UserUpdateStatusReqVO reqVO) {
        systemUserService.updateUserStatus(reqVO.getId(), reqVO.getStatus());
        return success(true);
    }

    @PostMapping("/page")
    @OperateLog(enable = false)
    @Operation(summary = "获得用户分页列表")
    public Result<PageResult<UserRespVO>> getUserPage(@RequestBody UserPageReqVO pageReqVO) {
        return success(systemUserService.getUserPage(pageReqVO));
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户详情")
    public Result<UserRespVO> getUser(@RequestParam("id") Long id) {
        return success(systemUserService.getUserDetail(id));
    }

}
